package com.zktr.dao;


import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.zktr.pojo.Category;
import com.zktr.pojo.Goods;
import com.zktr.utils.CRUDDAO;

/**
 * 分类dao层
 *
 */
public class CategoryDao {
	
	private CRUDDAO<Category> crud=new CRUDDAO<>(Category.class);
	private CRUDDAO<Goods> goods=new CRUDDAO<>(Goods.class);
	/**
	 * 查询父分类
	 * @return
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 */
	public static void main(String[] args) throws NoSuchFieldException, SecurityException {
		CategoryDao categoryDao = new CategoryDao();
	    System.out.println(categoryDao.selectCateGoods("手机"));
	    XlsDto2Excel xlsDto2Excel = new XlsDto2Excel();
		try {
			String xlsDto2Excel2 = xlsDto2Excel.xlsDto2Excel(categoryDao.selectCateGoods("手机"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public Map<Integer, List<Category>> selectParent() {
		Map<String, Object []> map=new HashMap<>();
		map.put("select * from category where parentid=0 order by catid", null);
		return crud.selectAll(map);		
	}
	/**
	 * 查询子分类
	 * @return 
	 */
	public Map<Integer, List<Category>> selectChilds(int id) {
		Map<String, Object []> map=new HashMap<>();
		map.put("select * from category where parentid=? order by catid", new Object[] {id});
		return crud.selectAll(map);		
	}
	/**
	 * 模糊查询
	 * @return 
	 */
	public List<Category> selectLike(String name) {
		String sql="select * from category where catname like '%"+name+"%' and parentid != 0 limit 0,5";
		return crud.select(sql);
	}
	public int queryAllcount() {
		String sql="select count(*) from category";
		return crud.selectCount(sql);
	}
/*	public List<CategoryBean> queryAllCategory(int ps,int pc) {
		Object[] obj={(pc-1)*ps,pc};
		HashMap<String, Object[]> map = new LinkedHashMap<>();
		map.put("select * from category  limit ?,?", obj);
		List<Category> list = crud.selectAll(map).get(0);
		List<CategoryBean> categoryBean = new ArrayList<CategoryBean>(); 
		BeanCopyUtils beanCopyUtils = new BeanCopyUtils();
		beanCopyUtils.populate(list,categoryBean);	
		return categoryBean;
	}*/
	/**
	 * 分页查询所有分类
	 * @param ps
	 * @param pc
	 * @return
	 */
	public List<Category> queryAllCategory(int ps,int pc) {
		Object[] obj={(pc-1)*ps,pc};
		LinkedHashMap<String, Object[]> map = new LinkedHashMap<>();
		map.put("select * from category  limit ?,?", obj);
		return crud.selectAll(map).get(0);
	}
	/**查询所有类别数量
	 * @return
	 */
	public int  selectCategoryCount() {
		return crud.selectCount("select count(*) from category");
	}
	/**
	 * 查询分类
	 * @return
	 */
	public List<Category> queryAllCategoryName() {
		
		LinkedHashMap<String, Object[]> map = new LinkedHashMap<>();
		map.put("select * from category  limit 230", null);
		return crud.selectAll(map).get(0);
	}
	public int selectCateName(String catname) {
		Object object[]={catname};
		LinkedHashMap<String, Object[]> map = new LinkedHashMap<>();
		String sql="select  * from category where catname=?";
		map.put(sql,object);
		List<Category> list = crud.selectAll(map).get(0);
		for (Category category : list) {
		return	category.getCatid();
		}
		return 0;
	}
	
	public List<Goods> selectCateGoods(String catname) {
		CategoryDao categoryDao = new CategoryDao();
		int catid = categoryDao.selectCateName(catname);
		Object object[]={catid};
		LinkedHashMap<String, Object[]> map = new LinkedHashMap<>();
		String sql="select * from goods where catid=?";
	    map.put(sql,object);
	   	return goods.selectAll(map).get(0);
	}
	public String fileaddress(String catname) throws Exception {
		CategoryDao categoryDao = new CategoryDao();
	    XlsDto2Excel xlsDto2Excel = new XlsDto2Excel();
		return  xlsDto2Excel.xlsDto2Excel(categoryDao.selectCateGoods(catname));
	}

}
